﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using KPIS.GERP.GHRMS.APP.Classes;
using Telerik.Web.UI;
using KPIS.Utils;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PS_HisRoyalItems : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private IEmployeeBLL empBLL = new EmployeeBLL();
        private IPositionBLL posBLL = new PositionBLL();
        private IRoyalItemHistoriesBLL royalHis = new RoyalItemHistoriesBLL();
        private EmployeeInfo empInfo = new EmployeeInfo();
        private RoyalItemHistoryInfo royalHisInfo;
        private RadComboBox cmbRoyalItem;
		private Main main = new Main();
        private object _dataItem = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            cmbRoyalItem = (RadComboBox)cbRoyalItem.FindControl("cbRoyalItem");
            lblEmpSeq.Text = Session["emp_seq"].ToString();

            if (DataItem != null)
            {
                cbRoyalItem.ListRoyalItemComboBox();

                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    IList<RoyalItemHistoryInfo> royalList = royalHis.ListRoyalItemHistoriesBySequence(Int32.Parse(DataBinder.Eval(DataItem, "Sequence").ToString()));

                    if (royalList.Count > 0)
                    {
                        royalHisInfo = royalList[0];
                        cmbRoyalItem.SelectedValue = royalHisInfo.RoyalItem.Sequence.ToString();
                        txtRoyalDate.SetValue(string.Format("{0:dd/MM/yyyy}", royalHisInfo.RoyalDate));
                        txtReceivedDate.SetValue(string.Format("{0:dd/MM/yyyy}", royalHisInfo.ReceiveDate));
                        txtGazetteNo1.Text = royalHisInfo.GazetteNo1;
                        txtGazetteNo2.Text = royalHisInfo.GazetteNo2;
                        txtGazetteNo3.Text = royalHisInfo.GazetteNo3;
                        txtGazetteNo4.Text = royalHisInfo.GazetteNo4;
                        txtGazetteNo5.Text = royalHisInfo.GazetteNo5;
                        txtGazetteDate.SetValue(string.Format("{0:dd/MM/yyyy}", royalHisInfo.GazetteDate));
                    }
                }
                else
                {

                }
            }
        }
        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }

        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            IList<EmployeeInfo> infoList = empBLL.ListEmployees(lblEmpSeq.Text.Trim());
            if (infoList.Count > 0)
            {
                empInfo = infoList[0];
            }

            royalHis.Insert(lblEmpSeq.Text.Trim()
                , (int?)null
                , cmbRoyalItem.SelectedValue
                , txtReceivedDate.GetStringValue().Length > 0 ? DateTime.Parse(txtReceivedDate.GetStringValue()) : (DateTime?)null
                , empInfo.Position.RefNo
                , empInfo.Position.Name
                , empInfo.Class.Name
                , empInfo.Salary
                , empInfo.Position.JobSite.Name
                , txtGazetteNo1.Text
                , txtGazetteNo2.Text
                , txtGazetteNo3.Text
                , txtGazetteNo4.Text
                , txtGazetteNo5.Text
                , txtGazetteDate.GetStringValue().Length > 0 ? DateTime.Parse(txtGazetteDate.GetStringValue()) : (DateTime?)null
                , txtRoyalDate.GetStringValue().Length > 0 ? DateTime.Parse(txtRoyalDate.GetStringValue()) : (DateTime?)null
				, ssMng.EmpSeqI);
			main.MessageAlert(Page, "บันทึกข้อมูลแล้ว");
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            IList<EmployeeInfo> infoList = empBLL.ListEmployees(lblEmpSeq.Text.Trim());
            if (infoList.Count > 0)
            {
                empInfo = infoList[0];
            }
            royalHis.Update(Int32.Parse(lblSeq.Text.Trim())
                , lblEmpSeq.Text.Trim()
                , (int?)null
                , cmbRoyalItem.SelectedValue
                , txtReceivedDate.GetStringValue().Length > 0 ? DateTime.Parse(txtReceivedDate.GetStringValue()) : (DateTime?)null
                , empInfo.Position.RefNo
                , empInfo.Position.Name
                , empInfo.Class.Name
                , empInfo.Salary
                , empInfo.Position.JobSite.Name
                , txtGazetteNo1.Text
                , txtGazetteNo2.Text
                , txtGazetteNo3.Text
                , txtGazetteNo4.Text
                , txtGazetteNo5.Text
                , txtGazetteDate.GetStringValue().Length > 0 ? DateTime.Parse(txtGazetteDate.GetStringValue()) : (DateTime?)null
                , txtRoyalDate.GetStringValue().Length > 0 ? DateTime.Parse(txtRoyalDate.GetStringValue()) : (DateTime?)null
				, ssMng.EmpSeqI);
			main.MessageAlert(Page, "บันทึกข้อมูลแล้ว");
        }
    }
}